package com.yixin.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.bo.PageInfo;
import com.yixin.hospital.entity.domain.User;
import com.yixin.hospital.entity.dto.req.user.LoginReqDTO;
import com.yixin.hospital.entity.dto.req.user.RegisterReqDTO;
import com.yixin.hospital.entity.dto.req.user.UserEditReqDTO;
import com.yixin.hospital.entity.vo.LoginVO;
import com.yixin.hospital.entity.vo.UserDeptWorkAddressJobOfficeVO;
import com.yixin.hospital.entity.vo.UserDetailVO;
import com.yixin.hospital.entity.vo.UserListVO;
import com.yixin.hospital.service.UserService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "员工管理", tags = "员工管理")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    @ApiOperation(value = "注册")
    @Transactional
    public ApiResult<?> register(@Valid @RequestBody RegisterReqDTO registerReqDTO) throws Exception {
        return userService.register(registerReqDTO);
    }

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public ApiResult<LoginVO> login(@Valid @RequestBody LoginReqDTO loginReqDTO, HttpServletRequest request) {
        return userService.login(loginReqDTO, request);
    }

    @GetMapping("/list")
    @ApiOperation(value = "员工列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", paramType = "query"),
            @ApiImplicitParam(name = "menu", value = "权限", paramType = "query"),
            @ApiImplicitParam(name = "jobDutyParam", value = "职务", paramType = "query"),
    })
    public ApiResult<PageInfo<UserListVO>> list(@RequestParam int currentPage, @RequestParam int pageSize, @RequestParam(required = false) String name,
                                                @RequestParam(required = false) Long jobId, @RequestParam(required = false) Long jobTitleId,
                                                @RequestParam(required = false) Long queryJobDutyId, @RequestParam(required = false) Integer status,
                                                HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return userService.allList(currentPage, pageSize, name, jobId, jobTitleId, queryJobDutyId, status, userId);
    }

    @GetMapping("/allList")
    @ApiOperation(value = "所有员工下拉框")
    public ApiResult<?> allList() {
        return ApiResult.success(userService.list(new LambdaQueryWrapper<User>().orderByAsc(User::getName)));
    }

    @GetMapping("/list-user-detail/{userId}")
    @ApiOperation(value = "列表用户详情")
    public ApiResult<UserDetailVO> listUserDetail(@PathVariable Long userId) throws Exception {
        return userService.listDetail(userId);

    }

    @GetMapping("/current-user-detail")
    @ApiOperation(value = "登录用户详情")
    public ApiResult<UserDetailVO> currentUserDetail(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return userService.loginDetail(userId);
    }

    @PostMapping("/edit")
    @ApiOperation(value = "编辑员工信息")
    @Transactional
    public ApiResult<?> edit(@Valid @RequestBody UserEditReqDTO userEditReqDTO, HttpServletRequest request) {
        return userService.edit(userEditReqDTO);
    }

    @GetMapping("/jobDuty-user/{jobDuty}")
    @ApiOperation(value = "根据职务查询人员列表")
    public ApiResult<List<User>> jobDutyUser(@PathVariable Long jobDuty) {
        List<User> list = userService.list(new LambdaQueryWrapper<User>().eq(User::getJobDuty, jobDuty));
        return ApiResult.success(list);
    }

    @GetMapping("/user-dept-workAddress-jobOffice/{phoneNumber}")
    @ApiOperation(value = "根据手机号查询用户部门和科室")
    public ApiResult<UserDeptWorkAddressJobOfficeVO> userDeptWorkAddressJobOffice(@PathVariable String phoneNumber) {
        return userService.userDeptWorkAddressJobOffice(phoneNumber);
    }

    @GetMapping("/dept-userList/{deptId}")
    @ApiOperation(value = "根据部门查询登录的医生列表，职务里有'医生'字样的user")
    public ApiResult<?> userDeptWorkAddressJobOffice(@PathVariable Long deptId) {
        return userService.userDeptWorkAddressJobOffice(deptId);
    }

    @GetMapping("/user-login-info/{userId}")
    @ApiOperation(value = "根据userid 查询登录信息")
    public ApiResult<UserDetailVO> userLoginInfo(@PathVariable Long userId) {
        return userService.userLoginInfo(userId);
    }

}
